import {ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot} from "@angular/router";
import {Product} from "../product/product.component";
import {Observable} from "rxjs/Observable";
import {Injectable} from "@angular/core";
//需要用装饰器注入
@Injectable()
export class ProductResolve implements Resolve<Product> {
    constructor(private router: Router) {
    }

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Product {

        //接收商品id
        let pid: number = route.params['id'];
        if (pid == 1) {
            //根据商品id,请求服务器拿商品数据,这里就不做请求了,只是演示一下
            return new Product(pid, '测试商品', ['aa'], 'imgurl', 2.5);
        } else {
            //没有商品id的时候,跳回home页面
            this.router.navigate(['/home']);
            return undefined;
        }
    }
}